package com.test.index;

import com.test.demo.entity.User;
import com.test.demo.repository.UserRepository;
import com.test.shiro.utils.JwtUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.HashMap;
import java.util.Map;
import java.util.concurrent.TimeUnit;

@RestController
public class LoginController {

    @Autowired
    private UserRepository userRepository;

    @Autowired
    private RedisTemplate redisTemplate;


    @GetMapping("/login/")
    public Map login(String name, String password) {
        User user = userRepository.findByNameAndPassWord(name, password);
        String token = JwtUtil.sign(name, password);
        redisTemplate.opsForValue().set(token, user);
        redisTemplate.expire(token, 4, TimeUnit.HOURS);
        Map<String, Object> map = new HashMap<>();
        map.put("accessToken", token);
        map.put("user", user);
        return map;
    }
}
